package com.ars.common.servlet.admin;

import com.ars.common.dao.UpdateUserInfo;
import com.ars.common.beans.User.User;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

@WebServlet("/UpdateUserInfoServlet")
public class UpdateUserInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
    {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
    {
        request.setCharacterEncoding("UTF-8");
        String Name = request.getParameter("Name");
        String UserID = request.getParameter("UserID");
        String Password = request.getParameter("Password");
        String Sex = request.getParameter("Sex").equals("男") ? "1" : "0";
        String Phone = request.getParameter("Phone");
        String Mailbox = request.getParameter("Mailbox");
        String id = request.getParameter("id");//没变化前的学号，避免学号修改找不到

//        System.out.println(Name);

        System.out.println(id);

        User user = new User();
        user.setName(Name);
        user.setUserID(UserID);
        user.setPassword(Password);
        user.setSex(Sex);
        user.setPhone(Phone);
        user.setMailbox(Mailbox);

//        System.out.println(user.toString());

        UpdateUserInfo updateUserInfo = new UpdateUserInfo();
        try {
            if(updateUserInfo.updateUserInfo(user,id))
            {
                System.out.println("修改成功");
//                request.getRequestDispatcher("/admin/userInfo.jsp").forward(request, response);
                String url = request.getContextPath()+ "/UserInfo";
                response.sendRedirect(url);//跳转
            }
            else
            {
                System.out.println("修改失败");
                response.sendRedirect("");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
